//package br.ufrn.opencopi.underlayIntegrationLayer.drivers;
//
//import java.net.InetSocketAddress;
//import java.util.Hashtable;
//
//import br.ufrn.opencopi.exceptions.HostException;
//import br.ufrn.opencopi.exceptions.PortException;
//import br.ufrn.opencopi.exceptions.UnknownMiddlewareTypeException;
//import br.ufrn.opencopi.middlewareInterface.drivers.contextConverters.ContextDataConverterInterface;
//import br.ufrn.opencopi.serviceLayer.serviceLayer.serviceManager.ServiceManager;
//import br.ufrn.opencopi.underlayIntegrationLayer.UnderlayIntegrationInterface;
//import br.ufrn.opencopi.util.PortController;
//
//import moca.protocol.eci.client.ECIClient;
//import moca.protocol.eci.client.listenersmanager.EventListener;
//import moca.service.cis.client.CisSubscriber;
//import moca.util.eci.ECIConstants;
//import moca.util.eci.Event;
//import moca.util.eci.Topic;
//
//public class MocaDriver extends GenericDriver implements UnderlayIntegrationInterface, ContextDataConverterInterface, EventListener{
//
//	Topic topic;
//	ECIClient mocaClient;
//
//	public MocaDriver(String middlewareName) throws HostException, PortException {
//		super(middlewareName);
//		InetSocketAddress eciUnderlayMiddlewarelAddress = new InetSocketAddress(middlewareMetadata.get("host"),Integer.parseInt(middlewareMetadata.get("port")));
//		InetSocketAddress eciOpencopiAddress = new InetSocketAddress(ServiceManager.getMiddlewareMetadata("opencopi").get("host"),PortController.getValidPort());
//
//		try {
//
//			if(eciUnderlayMiddlewarelAddress.getHostName().equals(null) || eciOpencopiAddress.getHostName().equals(null)){
//				throw new HostException("MocaDriver: Host cannot be null");
//			}
//
//			if(eciUnderlayMiddlewarelAddress.getPort()<0 || eciUnderlayMiddlewarelAddress.getPort() > 65000){
//				throw new PortException("MocaDriver: Invalid Port");
//			}
//
//			mocaClient = new CisSubscriber(ECIConstants.TCP, eciUnderlayMiddlewarelAddress, eciOpencopiAddress);
//		} catch (Exception e) {
//			System.out.println("MocaDriver: "+e.getMessage());
//			e.printStackTrace();
//		}
//		// TODO Auto-generated constructor stub
//	}
//
//	//MiddInterface methods
//	public Object getContextData() {
//		// TODO Auto-generated method stub
//		return null;
//	}
//
//	public int register() {
//		// TODO Auto-generated method stub
//		return 0;
//	}
//
//	public boolean subscribe(String subject, Hashtable conditions) {
//		try {
//			makeExpressionConditions();
//			//topic = clienteMoca.subscribe(lf.getSubject(), lf.stringSubscription);
//			topic = clientMoca.subscribe(lf.getSubject(), conditions);//subject and conditions
//			clientMoca.addListener(this, topic);
//			idSubscription = topic.getExpression();
//			System.out.println("Moca: Expressao: "+topic.getExpression()+" e subject: "+topic.getSubject());
//			return true;
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return false;
//	}
//	public void makeExpressionConditions() {
//		ie = lf.getInternalEvents().iterator();
//		exp = (Expression)ie.next();
//		//a exp deve ser, por exemplo:
//		//((EnergyLevel < 65) and (FreeMemory < 18000) and (OnLine = false))
//		conditions = "("+exp.getComplete_exp()+")";
//		int count = 1;
//		while(ie.hasNext()){
//			count++;
//			exp = (Expression)ie.next();
//			conditions = conditions+" and ("+exp.getComplete_exp()+")";
//		}
//		//if has more tah one conditions then is necessary put among to '(' e ')'
//		if(count > 1){
//			conditions = "("+conditions+")";
//		}
//
//
//	}
//
//	public boolean unRegister(int id) {
//		// TODO Auto-generated method stub
//		return false;
//	}
//
//	public boolean unSubscribe(int id) {
//		mocaClient.unsubscribe(topic);
//		if (mocaClient.removeListeners(topic))
//			return true;
//		else
//			return false;
//	}
//
//	public void callback(Object data) {
//		
//		EventProperties evProp = (EventProperties)evt.getData();
//		Enumeration en = evProp.getPropertyNames();
//		Attributes mocaAtt;
//
//
//		while(en.hasMoreElements()){
//			att = new ContextAttribute();
//			String nameProp = (String)en.nextElement();
//			mocaAtt = evProp.getAtribute(nameProp);
//			if(mocaAtt.getType().equals("STRING")){
//				att.setName(mocaAtt.getName());
//				att.setType(ContextAttribute.STRING);
//				att.setValue((String)mocaAtt.getValue());
//			}else if(mocaAtt.getType().equals("FLOAT")){
//				att.setName(mocaAtt.getName());
//				att.setType(ContextAttribute.FLOAT);
//				att.setValue(mocaAtt.getValue());
//			}else if(mocaAtt.getType().equals("DOUBLE")){
//				att.setName(mocaAtt.getName());
//				att.setType(ContextAttribute.DOUBLE);
//				att.setValue(mocaAtt.getValue());
//			}else if(mocaAtt.getType().equals("LONG")){
//				att.setName(mocaAtt.getName());
//				att.setType(ContextAttribute.LONG);
//				att.setValue(mocaAtt.getValue());
//			}else if(mocaAtt.getType().equals("BOOLEAN")){
//				att.setName(mocaAtt.getName());
//				att.setType(ContextAttribute.BOOLEAN);
//				att.setValue(mocaAtt.getValue());
//			}else if(mocaAtt.getType().equals("INTEGER")){
//				att.setName(mocaAtt.getName());
//				att.setType(ContextAttribute.INTEGER);
//				att.setValue(mocaAtt.getValue());
//			}
//			eventInformations.put(att.getName(), att);
//		}
//	}
//
//	//ContextDataConverterInterface methods
//	public Object convertOpencopiModelToUnderlayModel(Object opencopiData) {
//		// TODO Auto-generated method stub
//		return null;
//	}
//
//	public Object convertUnderlayModelToOpencopiModel(Object underlayData) {
//		// TODO Auto-generated method stub
//		return null;
//	}
//
//	//Moca EventListener method
//	public void onReceiveData(Event event) {
//		//It just repassing to callback method. This method is MoCA specific.
//		callback(event);
//	}
//
//	@Override
//	public void registryMiddleware(String name, int type,
//			Hashtable<String, String> properties)
//			throws UnknownMiddlewareTypeException {
//		// TODO Auto-generated method stub
//		
//	}
//
//	@Override
//	public void unregistry(String name) {
//		// TODO Auto-generated method stub
//		
//	}
//
// }
